package ch4;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import lombok.Getter;
import lombok.Setter;
import junit.framework.Test;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class FileReaderTester extends TestCase{
    @Getter
    @Setter
    private Reader _input;


    public FileReaderTester(String name){
        super(name);
    }

    @Override
    protected void setUp(){
        try{
           _input = new FileReader("data.txt");
        }catch (Exception e){
            throw new RuntimeException("unable to open test file");
        }
    }

    @Override
    protected void tearDown() throws Exception {
        try{
            _input.close();
        }catch (Exception e){
            throw new RuntimeException("error on closing test file");
        }
    }

    public void testRead() throws IOException{
        char ch = '&';
        for (int i = 0; i < 4; i++) {
            ch =(char) _input.read();
        }
        assertEquals('d', ch);
    }

    public  void testReadAtEnd() throws IOException {
        int ch = -1234;
        for (int i = 0; i < 204; i++) {
            ch = _input.read();
        }
        assertEquals("read last char",'6',_input.read());
        assertEquals(-1,_input.read());
    }


    public static Test suite(){
        TestSuite suite = new TestSuite();
        suite.addTest(new FileReaderTester("testRead"));
        suite.addTest(new FileReaderTester("testReadAtEnd"));
        return suite;
    }

    public static void main(String[] args){
//        junit.textui.TestRunner.run(suite());
        junit.textui.TestRunner.run(new TestSuite(FileReaderTester.class));
    }
}
